package com.hskn.hss.module.equiptype.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.ListEntity;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.equiptype.entity.TlEquiptype;
import com.hskn.hss.module.equiptype.mapper.TlEquiptypeMapper;
import com.hskn.hss.module.equiptype.service.ITlEquiptypeService;
import com.hskn.hss.module.tlcartype.entity.TlCarType;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Service
public class TlEquiptypeServiceImpl extends ServiceImpl<TlEquiptypeMapper, TlEquiptype> implements ITlEquiptypeService {

    @Resource
    TlEquiptypeMapper tlEquiptypeMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(TlEquiptype tlEquiptype) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlEquiptype.setCreateby(uid);
        tlEquiptype.setCreatetime(new Date());
        tlEquiptype.setDeptid(deptid);
        int i = tlEquiptypeMapper.insert(tlEquiptype);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEquiptype tlEquiptype = params.getObject("data", TlEquiptype.class);
        tlEquiptype.setUpdateby(uid);
        tlEquiptype.setUpdatetime(new Date());
        int i = tlEquiptypeMapper.updateById(tlEquiptype);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlEquiptypeMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public List<ListEntity> getSelectList() {
        List<ListEntity> resultList = tlEquiptypeMapper.getSelectList();
        return resultList;
    }
}
